package com.ruoyi.mixuebingcheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mixuebingcheng.domain.bo.AddressBookBo;
import com.ruoyi.mixuebingcheng.domain.vo.AddressBookVo;
import com.ruoyi.mixuebingcheng.domain.AddressBook;
import com.ruoyi.mixuebingcheng.mapper.AddressBookMapper;
import com.ruoyi.mixuebingcheng.service.IAddressBookService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地址管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RequiredArgsConstructor
@Service
public class AddressBookServiceImpl implements IAddressBookService {

    private final AddressBookMapper baseMapper;

    /**
     * 查询地址管理
     */
    @Override
    public AddressBookVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询地址管理列表
     */
    @Override
    public TableDataInfo<AddressBookVo> queryPageList(AddressBookBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AddressBook> lqw = buildQueryWrapper(bo);
        Page<AddressBookVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地址管理列表
     */
    @Override
    public List<AddressBookVo> queryList(AddressBookBo bo) {
        LambdaQueryWrapper<AddressBook> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AddressBook> buildQueryWrapper(AddressBookBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AddressBook> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AddressBook::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getConsignee()), AddressBook::getConsignee, bo.getConsignee());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), AddressBook::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getDetail()), AddressBook::getDetail, bo.getDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), AddressBook::getLabel, bo.getLabel());
        lqw.eq(bo.getIsDefault() != null, AddressBook::getIsDefault, bo.getIsDefault());
        lqw.eq(bo.getIsDeleted() != null, AddressBook::getIsDeleted, bo.getIsDeleted());
//        lqw.eq(bo.getCreateUser() != null, AddressBook::getCreateUser, bo.getCreateUser());
//        lqw.eq(bo.getUpdateUser() != null, AddressBook::getUpdateUser, bo.getUpdateUser());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaCode()), AddressBook::getAreaCode, bo.getAreaCode());
        return lqw;
    }

    /**
     * 新增地址管理
     */
    @Override
    public Boolean insertByBo(AddressBookBo bo) {
        AddressBook add = BeanUtil.toBean(bo, AddressBook.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改地址管理
     */
    @Override
    public Boolean updateByBo(AddressBookBo bo) {
        AddressBook update = BeanUtil.toBean(bo, AddressBook.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AddressBook entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地址管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
